﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using DLLTest.OpertionClass;
using System.Data;
using System.Windows.Forms;

namespace DLLTest.OpretionClass
{
    class TreeViewOpertion
    {
        //数据连接
        public TreeView treeView3;
        #region 获得数据库信息放入TreeView中(递归)
        /// <summary>
        /// 获得数据库信息放入TreeView中(递归)
        /// </summary>
        /// <param name="ParentID"></param>
        /// <param name="parentNode"></param>
        public void AddTreeView(int ParentID, TreeNode parentNode)
        {
            DataOperate dataoper = new DataOperate();
            string querySql = "select * from TreeNode where Status =" + 1;
            DataView dataTree = new DataView(dataoper.GetDataSet(querySql).Tables[0]);
            dataTree.RowFilter = "ParentID=" + ParentID;
            foreach (DataRowView row in dataTree)
            {
                TreeNode node = new TreeNode()
                {
                    ToolTipText = row["ExceptChannel"].ToString(), // 暂存排除的通道号
                    Name = row["ID"].ToString(),
                    Text = row["TreeText"].ToString(),
                    Tag = string.Format("{0},{1},{2},{3}", row["TreeName"].ToString(), row["DriverPort"].ToString(),
                            row["DriverName"].ToString(), row["DriverPwd"].ToString()),
                };

                if (parentNode == null) //处理根节点
                    treeView3.Nodes.Add(node);
                else //处理子节点
                    parentNode.Nodes.Add(node);

                AddTreeView(Int32.Parse(row["ID"].ToString().Trim()), node);
            }
        }
        #endregion
        ////public void AddTreeView2(string ParentID, TreeNode parentNode)
        ////{
        ////    DataOperate dataoper = new DataOperate();
        ////    //string querySql = "select * from TreeNode where ParentID="+ParentID+" and Status =" + 1;
        ////    //string querySql = "select * from TreeNode" ;
        ////    string querySql = "select * from TreeNode where ParentID='" + ParentID+"'";
        ////    DataView dataTree = new DataView(dataoper.GetDataSet(querySql).Tables[0]);
        ////    string filter = "ParentID=" + ParentID;
        ////    dataTree.RowFilter = filter;
        ////    foreach (DataRowView row in dataTree)
        ////    {
        ////        TreeNode node = new TreeNode();
        ////        //处理根节点
        ////        if (parentNode == null)
        ////        {
        ////            node.Name = row["ID"].ToString();
        ////            node.Text = row["TreeName"].ToString();
        ////            TreeView treeview = new TreeView();
        ////            treeview.Nodes.Add(node);
        ////            //AddTreeView(Int32.Parse(row["ID"].ToString().Trim()), node);
        ////        }
        ////        else //处理子节点
        ////        {
        ////            node.Name = row["ID"].ToString();
        ////            node.Text = row["TreeName"].ToString();
        ////            parentNode.Nodes.Add(node);
        ////            //AddTreeView(Int32.Parse(row["ID"].ToString().Trim()), node);
        ////        }
        ////    }
        //}        
        ///// <summary>
        ///// 删除所选节点及其所有子节点 (通过遍历节点删除对应的数据库数据)
        ///// </summary>
        ///// <param name="parentNode"></param>
        //public void DeleteTreeNode(TreeNode parentNode)
        //{
        //    //方法一：删除选中节点及其所有子节点
        //    int childCount = parentNode.Nodes.Count;
        //    for (int i = 0; i < childCount; i++)
        //    {
        //        TreeNode childNode = parentNode.Nodes[0];
        //        if (childNode.Nodes.Count > 0)
        //        {
        //            DeleteTreeNode(childNode);
        //        }
        //        else
        //        {
        //            DeleteNodeData(childNode);
        //            childNode.Remove();
        //        }
        //    }
        //    if (parentNode.Nodes.Count == 0)
        //    {
        //        DeleteNodeData(parentNode);
        //        parentNode.Remove();
        //    }
        //    //方法二：删除选中节点及其所有子节点******
        //    //TreeNodeCollection treeNodeCollection = parentNode.Nodes;
        //    //int count = treeNodeCollection.Count;
        //    //for (int i = 0; i < count; i++)
        //    //{
        //    //    TreeNode node = treeNodeCollection[0];
        //    //    if (node.Nodes.Count > 0)
        //    //    {
        //    //        DeleteTreeNode(node);
        //    //    }
        //    //    else
        //    //    {
        //    //        DeleteNodeData(node);
        //    //        node.Remove();
        //    //    }
        //    //}
        //    //if (treeNodeCollection.Count == 0)
        //    //{
        //    //    DeleteNodeData(parentNode);
        //    //    parentNode.Remove();
        //    //}
        //}

        /////<summary>
        ///// 方法三：删除选中节点的所有子节点（选中的主节点不会被删除）
        ///// </summary>
        ///// <param name="parentNode">所选节点</param>
        //public void DeleteTreeNodeWithoutPareNode(TreeNode parentNode)
        //{
        //    int count = parentNode.Nodes.Count;
        //    for (int i = 0; i < count; i++)
        //    {
        //        TreeNode node = parentNode.Nodes[0];
        //        DeleteTreeNodeWithoutPareNode(node);
        //        DeleteNodeData(node);
        //        node.Remove();
        //    }
        //}

        ///// <summary>
        ///// 将要删除的节点传给该函数，利用节点的Name属性(ID)删除数据库数据
        ///// </summary>
        ///// <param name="node"></param>
        //public void DeleteNodeData(TreeNode node)
        //{
        //    DataOperate sqlManager = new DataOperate();
        //    string deleteSqlCmd = String.Empty;
        //    deleteSqlCmd = "delete from TreeNode where ID = " + node.Name;
        //    sqlManager.RunSqlCommand(deleteSqlCmd);
        //}     
    }
}
/*
/判断选定的节点是否存在下一级节点
if ( treeView1.SelectedNode.Nodes.Count == 0 )
    //删除节点
    treeView1.SelectedNode.Remove ( ) ;
 else
    MessageBox.Show ( "请先删除此节点中的子节点！" , "提示信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
    }
    private void button2_Click ( object sender , System.EventArgs e )
    {
        //定位根节点
        treeView1.SelectedNode = treeView1.Nodes [ 0 ] ;
        //展开组件中的所有节点
        treeView1.SelectedNode.ExpandAll ( ) ;
    }
    private void button3_Click ( object sender , System.EventArgs e )
    {
        //定位根节点
        treeView1.SelectedNode = treeView1.Nodes [ 0 ] ;
        //折叠组件中所有节点
        treeView1.SelectedNode.Collapse ( ) ;
    }
*/

